namespace com.crownpack.initiatives.Features.WebLevel_InitiativesMaster
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;

    using com.crownpack.Exceptions.SharePointExceptions;
    using com.crownpack.Extensions.SharePointExtensions;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;

    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid( "13542d65-98ac-433b-a372-c1931286d148" )]
    public class WebLevel_InitiativesMasterEventReceiver : SPFeatureReceiver
    {
        #region Fields

        const string SITE = "i2e";

        static string _Title;

        #endregion Fields

        #region Properties

        public static Guid ID
        {
            get
            {
                return new Guid( "6e8c6827-66b5-4dbb-b610-13121f1440fd" ); // from feature manifest
            }
        }

        public static string Title
        {
            get
            {
                if ( string.IsNullOrEmpty( _Title ) )
                    _Title = SPFarm.Local.GetFeatureDefinition( ID ).GetTitle( );
                return _Title;
            }
        }

        #endregion Properties

        #region Methods

        public override void FeatureActivated( SPFeatureReceiverProperties properties )
        {
            try
            {
                if ( !( properties.Feature.Parent is SPWeb ) )
                {
                    throw new FeatureActivationFailedNotScopedForWebException( ID, Title, properties.GetWebUrl( ) );
                }

                if ( !isCorrectSite( properties ) )
                    throw new Exception( string.Format( "The feature cannot be activated in this website. To activate this feature, the website must be a child website of the root site and must be named '{3}'. The specified feature {0} ({1}) was not successfully activated at '{2}'.", ID, Title, properties.GetWebUrl( ), SITE ) );

                using ( InitiativesMasterList_Definition_WebLevel o = new InitiativesMasterList_Definition_WebLevel( properties ) )
                    o.Create( );
            }
            catch ( Exception e )
            {
                throw new FeatureActivationException( ID, Title, properties.GetWebUrl( ), e );
            }
        }

        public override void FeatureDeactivating( SPFeatureReceiverProperties properties )
        {
            try
            {
                if ( !( properties.Feature.Parent is SPWeb ) )
                {
                    throw new FeatureDeactivationFailedNotScopedForWebException( ID, Title, properties.GetWebUrl( ) );
                }

                using ( InitiativesMasterList_Definition_WebLevel o = new InitiativesMasterList_Definition_WebLevel( properties ) )
                    o.Delete( );
            }
            catch ( Exception e )
            {
                throw new FeatureDeactivationException( ID, Title, properties.GetWebUrl( ), e );
            }
        }

        /// <summary>
        /// The current structure must be only 2 levels and this web site must be the target site name.
        /// </summary>
        /// <param name="properties"></param>
        /// <returns></returns>
        bool isCorrectSite( SPFeatureReceiverProperties properties )
        {
            Uri parent = null;
            using ( SPWeb spWeb = properties.GetWeb( ) )
            {
                parent = new Uri( spWeb.ParentWeb.Url );
            }
            Uri current = new Uri( properties.GetWebUrl( ) );

            string p = string.Format( "{0}/{1}", parent.ToString( ), SITE );
            return p.Equals( current.ToString( ), StringComparison.OrdinalIgnoreCase );
        }

        #endregion Methods
    }
}